package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper contentMapper;


    @Override
    public void save(Travel travel) {
        // 设置默认值
        travel.setCreateTime(new Date());
        travel.setLastUpdateTime(new Date());
        travelMapper.insert(travel);
        // 获取自动生成的主键
        TravelContent travelContent = travel.getContent();
        travelContent.setId(travel.getId());
        contentMapper.insert(travelContent);
    }

    @Override
    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.queryForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void delete(Long id) {
        travelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Travel> selectAll() {
        return travelMapper.selectAll();
    }

    @Override
    public void changeState(Travel travel) {
        if(travel.getState() == Travel.STATE_RELEASE){
            // 设置发布的时间
            travel.setReleaseTime(new Date());
        }
        travelMapper.changeState(travel);
    }

    @Override
    public TravelContent getTravelById(Long travelId) {
        return contentMapper.selectByPrimaryKey(travelId);
    }

    @Override
    public List<Travel> selectTravelsBystrategyId(Long strategyId) {
        return travelMapper.selectTravelsBystrategyId(strategyId);
    }

    @Override
    public void deleteRelation(Long strategyId, Long travelId) {
        travelMapper.deleteRelation(strategyId,travelId);
    }

    @Override
    public List<Travel> getRegionTravelByStrategyId(Long strategyId) {
        return travelMapper.getRegionTravelByStrategyId(strategyId);
    }

    @Override
    public void saveRelation(Long strategyId, Long travelId) {
        travelMapper.saveRelation(strategyId,travelId);
    }

    @Override
    public Map<String, Object> getTravelAndStatus(Long userId, Long travelId) {
        return travelMapper.selectTravelAndStatus(userId,travelId);
    }

    @Override
    public List<Travel> getTravelsByUserId(Long userId) {
        return travelMapper.selectTravelsByUserId(userId);
    }


    @Override
    public void update(Travel travel) {
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);

        // 更新游记内容
        TravelContent content = travel.getContent();
        content.setId(travel.getId());
        contentMapper.updateByPrimaryKey(content);
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

}
